from django.db import models

# Create your models here.
from merchants.models import ShopsModel,GoodModel
from send.models import SendModel
# from order.models import OrderModel


# 会员表
class RoleModel(models.Model):
    name = models.CharField(verbose_name="名称", max_length=20)
    discount = models.DecimalField(verbose_name="折扣", max_digits=3,decimal_places=2)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="role_tb"
        verbose_name = "会员表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 折扣 %s"%(self.name,self.discount)



# 用户表
class UserModel(models.Model):
    nickname = models.CharField(verbose_name="昵称", max_length=20)
    avatar_url = models.CharField(verbose_name="用户头像", max_length=256, null=True)
    mobile = models.CharField(verbose_name='手机号', max_length=11)
    password = models.CharField(verbose_name="密码", max_length=256)
    role = models.ForeignKey(verbose_name="vip", to=RoleModel, on_delete=models.PROTECT)
    date_joined = models.DateTimeField(verbose_name="注册时间", auto_now_add=True)
    last_login = models.DateTimeField(verbose_name="上次登录时间", null=True)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="users_tb"
        verbose_name = "用户表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.nickname


# 地址表
class AddressModel(models.Model):
    user = models.ForeignKey(verbose_name="用户",to=UserModel, on_delete=models.CASCADE)
    address = models.CharField(verbose_name="地址",max_length=128)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="address_tb"
        verbose_name = "地址表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 地址 %s"%(self.user.nickname,self.address)


# 购物车
class UserShoppingModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    goods = models.ForeignKey(verbose_name="商品", to=GoodModel, on_delete=models.CASCADE)
    num = models.IntegerField(verbose_name="数量")
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table = "user_shopping_tb"
        verbose_name = "购物车表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 商家 %s 商品 %s" % (self.user.nickname, self.shops.name, self.goods.name)


# 收藏表
class CollectModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="collect_tb"
        verbose_name = "收藏表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 收藏 %s"%(self.user.nickname,self.shops.name)


# 优惠卷表
class CouponsModel(models.Model):
    COUPONS_TYPE = (
        (1, "商家"),
        (2, "平台"),
    )
    type = models.SmallIntegerField(verbose_name="优惠卷类型", choices=COUPONS_TYPE)
    name = models.CharField(verbose_name="名称", max_length=20)
    valid_time = models.DateTimeField(verbose_name="有效期")
    free_price = models.DecimalField(verbose_name="面额", max_digits=6, decimal_places=2)
    is_use = models.BooleanField(verbose_name="是否使用", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table = "coupons_tb"
        verbose_name = "优惠卷表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 面额 %s 有效期 %s" % (self.name, self.free_price, self.valid_time)


# 用户优惠卷表
class UserCouponsModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    coupons = models.ForeignKey(verbose_name="优惠卷", to=CouponsModel, on_delete=models.CASCADE)
    is_use = models.BooleanField(verbose_name="是否使用", default=False)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table = "user_coupons_tb"
        verbose_name = "用户优惠卷表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 优惠卷 %s 有效期 %s 是否使用 %s" % (self.user.nickname, self.coupons.name, self.coupons.free_price,self.is_use)


# 沟通表
class CommunicationModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE)
    send = models.ForeignKey(verbose_name="送餐员", to=SendModel, on_delete=models.CASCADE)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table = "communication_tb"
        verbose_name = "沟通表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "用户 %s 商家 %s 送餐员 %s" % (self.user.nickname, self.shops.name,self.send.nickname)


# 订单
class OrderModel(models.Model):
    ORDER_STATE = (
        (0, "未支付"),
        (1, "已支付"),
        (2, "超时支付失败"),
    )
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE)
    shops = models.ManyToManyField(verbose_name="商家",to=ShopsModel)
    goods = models.ManyToManyField(verbose_name="商品",to=GoodModel)
    send = models.ForeignKey(verbose_name="送餐员", to=SendModel, on_delete=models.CASCADE)
    pay_state = models.SmallIntegerField(verbose_name="订单状态", choices=ORDER_STATE, default=0)
    is_refund = models.BooleanField(verbose_name="是否退款", default=False)
    create_time = models.DateTimeField(verbose_name="创建时间", auto_now_add=True)
    pay_time = models.DateTimeField(verbose_name="支付时间", null=True)
    coupons = models.ManyToManyField(verbose_name="优惠卷", to=UserCouponsModel)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="order_tb"
        verbose_name = "订单表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "订单 %s %s 商家 %s"%(self.id,self.user.nickname,self.shops)


# 评论表
class CommentModel(models.Model):
    user = models.ForeignKey(verbose_name="用户", to=UserModel, on_delete=models.CASCADE,blank=True , null=True)
    shops = models.ForeignKey(verbose_name="商家", to=ShopsModel, on_delete=models.CASCADE,blank=True , null=True)
    send = models.ForeignKey(verbose_name="送餐员", to=SendModel, on_delete=models.CASCADE,blank=True , null=True)
    order = models.ForeignKey(verbose_name="订单", to=OrderModel, on_delete=models.CASCADE,blank=True , null=True)
    content = models.CharField(verbose_name="评论内容", max_length=256)
    is_delete = models.BooleanField(verbose_name="是否删除", default=False)

    class Meta():
        db_table="comment_tb"
        verbose_name = "收藏表"
        verbose_name_plural = verbose_name

    def __str__(self):
        return "%s 收藏 %s"%(self.user.nickname,self.shops.name)


class FoodModel(models.Model):
    url = models.CharField(verbose_name="图片路径", max_length=1000)
    name = models.CharField(verbose_name="名字", max_length=30)
    seasoning = models.CharField(verbose_name="调料", max_length=100)

    class Meta():
        db_table="food_tb"
        verbose_name = "美食表"

    def __str__(self):
        return self.name


